﻿using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Istace.Framework.Mapping.Core.Exceptions
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class MapperBaseException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MapperBaseException"/> class.
        /// </summary>
        public MapperBaseException()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MapperBaseException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public MapperBaseException(string message) : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MapperBaseException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="exception">The exception.</param>
        public MapperBaseException(string message, Exception exception) : base(message, exception)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MapperBaseException"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        protected MapperBaseException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        /// <summary>
        /// SourceType mapper type
        /// </summary>
        private Type SourceType { get; set; }

        /// <summary>
        /// DestinationType mapper type
        /// </summary>
        private Type DestinationType { get; set; }

        /// <summary>
        /// Default .ctor
        /// </summary>
        /// <param name="message">The exception message</param>
        /// <param name="source">SourceType type.</param>
        /// <param name="destination">DestinationType type.</param>
        protected MapperBaseException(string message, Type source, Type destination) : base(message)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (destination == null) throw new ArgumentNullException("destination");

            this.SourceType = source;
            this.DestinationType = destination;
        }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter" />
        ///   </PermissionSet>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if(info == null) throw new ArgumentNullException("info");
            info.AddValue("SourceType", SourceType);
            info.AddValue("DestinationType", DestinationType);
            base.GetObjectData(info, context);
        }
    }
}
